$(function() {
	$("input[id^='mold_s']:visible").autocomplete(Test.mold);

	Tip.emp('负责人');
	
	$("select[id^='emp_t_s']:visible").change(function() {
		var label = $("label[id^='label_emp_t']:visible");
		var a = $(this).val();
		if (a == 'emp') {
			label.html("计划人：");
		} else if (a == 'oper') {
			label.html("录入员：");
		} else if (a == 'worker') {
			label.html("负责人：");
		}
	});
	
	$("input[id^='worker_s']:visible").autocomplete(Test.emp);

	_ck_default("input[id^='worker_s']:visible", "input[id^='worker_t']:visible");
	
	_change("input[id='name_s']:visible", "select[id^='piece_t']:visible");
	
	$("select[id^='ab_s']:visible").change(function() {
		var s = $(this).val();
		// alert(s);
		if (s == '1') {			
			$("select[id^='status_s']").multiselect("widget").find(":checkbox").each(function() {
				var a = $(this).val();
				if ($(this).attr("checked")) {
					this.click();
				}
				
				if (a == '1') {
					this.click();
				}
			});
			
			$("select[id^='orderby_s']:visible").attr("value", '-');
			$("select[id^='desc_s']:visible").attr("value", '2');
		} else if (s == '2') {
			$("select[id^='status_s']").multiselect("widget").find(":checkbox").each(function() {
				var a = $(this).val();
				if ($(this).attr("checked")) {
					this.click();
				}
				
				if (a == '4') {
					this.click();
				}
			});
			
			$("select[id^='orderby_s']:visible").attr("value", '2');
			$("select[id^='desc_s']:visible").attr("value", '2');
		} else if (s == '3') {
			$("select[id^='status_s']").multiselect("widget").find(":checkbox").each(function() {
				var a = $(this).val();
				if ($(this).attr("checked")) {
					this.click();
				}
				
				if (a == '5' || a == '8') {
					this.click();
				}
			});
			
			$("select[id^='pagesize_s']:visible").attr("value", '50');
		} else {
			$("select[id^='status_s']").multiselect("widget").find(":checkbox").each(function() {
				var a = $(this).val();
				if ($(this).attr("checked")) {
					this.click();
				}
				
				if (a == '2' || a == '3') {
					this.click();
				}
			});
			
			$("select[id^='orderby_s']:visible").attr("value", 't2');
			$("select[id^='desc_s']:visible").attr("value", '1');
			$("select[id^='pagesize_s']:visible").attr("value", '15');
		}
	});
});

var PlanMy = {
	dialog_add_: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_add').dialog('option', 'buttons', {
			"关闭": function() { 
				$(this).dialog("close");
			},
			
			"添加": function() {
				if (!confirm("确认是否提交？")) {
					return false;
				}

				var form = $("#form_add")[0];

				var body = {};
				body.emp = form.emp.value;
				body.date = form.date.value;
				
				body.worker = form.worker.value;
				body.mold = form.mold.value;
				
				body.urgent = form.urgent.value;
				body.name = form.name.value;
				body.content = form.content.value;
				
				body.t1 = form.date1.value;
				body.t2 = form.date2.value;
				body.day = form.day.value;

				$(this).dialog("close");
				
				var url = 'mold.php?do=PlanMy.addPost';

				$.post(url, body, function(data) {
					$(result).html(data);
				});
			}
		});

		dialog_add(url, title, xy);
	},

	popup: function (e, p2, id, status) {
		$.cookie('id_', id);

		p2 = query(p2);
		$.cookie('p2', p2);

		var c1 = 'mold.php?do=PlanMy.edit&id=' + id;
		var c2 = 'mold.php?do=PlanMy.view&id=' + id;
		var c3 = 'mold.php?do=PlanMy.evaluation&id=' + id;
		
		var c4 = 'mold.php?do=PlanMy.status&id=' + id + p2;
		var c8 = 'mold.php?do=PlanMy.reason&id=' + id;

		var a1 = ['编辑信息', 'url', 'PlanMy.dialog_edit_', '650x420'];
		var a2 = ['查看信息', 'url', 'Common.dialog_view_', '650x420'];
		var a3 = ['评价任务', 'url', 'PlanMy.dialog_evaluation', '650x400'];
			
		// var a6 = ['暂停生产', 'url', 'Plan.dialog_stop', '650x420'];
		var a8 = ['取消任务', 'url', 'PlanMy.dialog_cancel', '650x420'];

		// 计划生产
		if (status == 1) {
			var d1 = 'mold.php?do=PlanMy.verify&id=' + id;
			var b1 = ['审核信息', 'url', 'PlanMy.dialog_verify', '650x400'];
			
			var s = d1 + ", " + c1 + ", " + c2;

			return this.popup_(e, [b1, a1, a2], s);
		}

		// 等待生产
		if (status == 2) {
			var d1 = 'mold.php?do=PlanMy.verify&id=' + id;
			var b1 = ['开始任务', 'url', 'PlanMy.dialog_begin', '650x400'];
			
			var s = d1 + ", " + c1 + ", " + c2 + ", " + c3 + ", " + c8;

			return this.popup_(e, [b1, a1, a2, a3, a8], s);
		}
		
		// 正在生产
		if (status == 3) {
			var d1 = 'mold.php?do=PlanMy.verify&id=' + id;
			var b1 = ['完成任务', 'url', 'PlanMy.dialog_finish', '650x400'];
			
			var s = d1 + ", " + c1 + ", " + c2 + ", " + c3 + ", " + c8;

			return this.popup_(e, [b1, a1, a2, a3, a8], s);
		}
		
		// 完成生产
		if (status == 4) {			
			var s = c3 + ", " + c1 + ", " + c2 + ", " + c8;

			return this.popup_(e, [a3, a1, a2, a8], s);
		}

		// 取消生产
		if (status == 8) {
			var b1 = ['继续任务', 'url', 'PlanMy.list_continue'];
			
			var s = c3 + "&status=3, " + c1 + ", " + c2;

			return this.popup_(e, [b1, a1, a2], s);
		}

		var s = c2;

		return this.popup_(e, [a2], s);
	},

	popup_: function (e, a, s) {
		var id = $.cookie('id_');
		
		var d2 = 'mold.php?do=PlanMy.copy&id=' + id;
		var b2 = ['复制信息', 'url', 'PlanMy.dialog_copy_', '650x420'];
		
		var ss = 'mold.php?do=PlanMy.ss&id=' + id;
		var bb = ['修改状态', 'url', 'PlanMy.dialog_ss', '400x200'];
		
		s += ", " + d2 + ", " + ss;
		a.push(b2);
		a.push(bb);
			
		return popup(e, a, s);
	},

	/**
	 * 修改状态
	 */
	dialog_ss: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('mold.php?do=PlanMy.ajax_emp', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() {
						$(this).dialog("close");
					},

					"修改": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}

						var form = $("#form_edit")[0];

						var body = {};
						body.id = form.id.value;
						body.field = form.field.value;
						body.data = form.data.value;
						body.status = form.status.value;

						$(this).dialog("close");

						var url = 'mold.php?do=PlanMy.ssPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});
	},

	dialog_edit_: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('mold.php?do=PlanMy.ajax_emp', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭" : function() {
						$(this).dialog("close");
					},

					"修改" : function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}

						var form = $("#form_edit")[0];

						var body = {};
						body.id = form.id.value;
						body.emp = form.emp.value;
						body.date = form.date.value;
						
						body.worker = form.worker.value;
						body.mold = form.mold.value;
						
						body.urgent = form.urgent.value;
						body.name = form.name.value;
						body.content = form.content.value;
						body.t1 = form.date1.value;
						body.t2 = form.date2.value;
						body.day = form.day.value;

						$(this).dialog("close");

						var url = 'mold.php?do=PlanMy.editPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});
	},

	dialog_copy_: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_add').dialog('option', 'buttons', {
			"关闭" : function() {
				$(this).dialog("close");
			},

			"复制" : function() {
				if (!confirm("确认是否提交？")) {
					return false;
				}

				var form = $("#form_add")[0];

				var body = {};
				body.emp = form.emp.value;
				body.date = form.date.value;
				
				body.worker = form.worker.value;
				body.mold = form.mold.value;
				
				body.urgent = form.urgent.value;
				body.name = form.name.value;
				body.content = form.content.value;
				
				body.t1 = form.date1.value;
				body.t2 = form.date2.value;
				body.day = form.day.value;

				$(this).dialog("close");

				var url = 'mold.php?do=PlanMy.addPost';

				$.post(url, body, function(data) {
					$(result).html(data);
				});
			}
		});

		dialog_add(url, title, xy);
	},

	view: function (id) {
		var url = "mold.php?do=PlanMy.view&id=" + id;
		$('#dialog_view').dialog('option', 'buttons', { 		
			"关闭": function() { 
				$(this).dialog("close");
			},
			
			"打印" : function() {
				var url = 'mold.php?do=PlanMy.view&tpl=doc&id=' + id;

				jswin(url, 'plan_print', '800x600');
			}
		});

		dialog_view(url, '计划信息', '600x400');
	},
	
	dialog_verify: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('mold.php?do=PlanMy.ajax_emp', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() { 
						$(this).dialog("close");
					},		

					"不提交": function() {
						if (!confirm("确认生产信息是否不提交？")) {
							return false;
						}				
						
						var form = $("#form_edit")[0];
						
						var body = {};
						body.id = form.id.value;
						body.email = form.email.value;
						body.content = form.content.value;
						body.status = 9;
						
						$(this).dialog("close");
						
						var url = 'mold.php?do=PlanMy.verifyPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					},
					
					"提交": function() {
						if (!confirm("确认生产信息是否提交？")) {
							return false;
						}				

						var form = $("#form_edit")[0];
						
						var body = {};
						body.id = form.id.value;
						body.email = form.email.value;
						body.content = form.content.value;
						body.status = 2;
						
						$(this).dialog("close");
						
						var url = 'mold.php?do=PlanMy.verifyPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});	
	},
	
	dialog_begin: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('mold.php?do=PlanMy.ajax_worker', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() { 
						$(this).dialog("close");
					},
					
					"开始" : function() {
						if (!confirm("确认是否开始生产？")) {
							return false;
						}				
	
						var form = $("#form_edit")[0];
						
						var body = {};
						body.id = form.id.value;
						body.email = form.email.value;
						body.content = form.content.value;
						body.status = 3;
						
						$(this).dialog("close");
						
						var url = 'mold.php?do=PlanMy.beginPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}
	
						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});
	
				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});
	},
	
	dialog_finish: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('mold.php?do=PlanMy.ajax_worker', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() { 
						$(this).dialog("close");
					},
					
					"完成" : function() {					
						if (!confirm("确认是否完成生产？")) {
							return false;
						}				
	
						var form = $("#form_edit")[0];
						
						var body = {};
						body.id = form.id.value;
						body.email = form.email.value;
						body.content = form.content.value;
						body.status = 4;
						
						$(this).dialog("close");
						
						var url = 'mold.php?do=PlanMy.finishPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}
	
						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});
	
				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});
	},
	
	dialog_evaluation: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('mold.php?do=PlanMy.ajax_emp', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() { 
						$(this).dialog("close");
					},
					
					"提交" : function() {					
						if (!confirm("确认是否提交？")) {
							return false;
						}				
	
						var form = $("#form_edit")[0];
						
						var body = {};
						body.id = form.id.value;
						body.evaluation = form.evaluation.value;
						body.email = form.email.value;
						body.content = form.content.value;
						body.status = 5;
						
						$(this).dialog("close");
						
						var url = 'mold.php?do=PlanMy.evaluationPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}
	
						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});
	
				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});
	},

	list_continue: function (url, view) {
		$.getJSON('mold.php?do=PlanMy.ajax_worker', function(json) {
			if (json.status == 1) {
				if (confirm("是否继续生产？")) {
					list(url, view);
				}
			} else {
				_error();
			}
		});
	},

	/**
	 * 暂停
	 */
	dialog_stop: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('mold.php?do=PlanMy.ajax_worker', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭" : function() {
						$(this).dialog("close");
					},

					"提交" : function() {
						if (!confirm("确认是否暂停？")) {
							return false;
						}

						var form = $("#form_edit")[0];

						var body = {};
						body.id = form.id.value;
						body.content = form.content.value;
						body.status = 3;

						$(this).dialog("close");

						var url = 'mold.php?do=PlanMy.reasonPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}

						$.post(url, body, function(data) {
							if ($("#barcode").val() == "1") {
								alert("操作成功！");
								$('#result').html('');
							} else {
								$(result).html(data);
							}
						});
					}
				});

				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});
	},

	/**
	 * 取消
	 */
	dialog_cancel: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('mold.php?do=PlanMy.ajax_worker', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭" : function() {
						$(this).dialog("close");
					},

					"提交" : function() {
						if (!confirm("确认是否取消生产？")) {
							return false;
						}

						var form = $("#form_edit")[0];

						var body = {};
						body.id = form.id.value;
						body.content = form.content.value;
						body.status = 8;

						$(this).dialog("close");

						var url = 'mold.php?do=PlanMy.reasonPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}

						$.post(url, body, function(data) {
							if ($("#barcode").val() == "1") {
								alert("操作成功！");
								$('#result').html('');
							} else {
								$(result).html(data);
							}
						});
					}
				});

				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});
	}, 

	list_: function () {
		var result = Common.result();
		var url = 'mold.php?do=PlanMy.findPost';

		var p2 = $.cookie('p2');
		if (p2 != null) {
			url += p2;
		}

		$.get(url, function(data) {
			$(result).html(data);
		});
	},
	
	popup_all: function (e, p2) {
		p2 = query(p2);
		$.cookie('p2', p2);

		var c1 = 'mold.php?do=PlanMy.delete' + p2;
		var c2 = 'mold.php?do=PlanMy.doc' + p2;
		var c9 = 'mold.php?do=PlanMy.xls' + p2;	
			
		var a1 = ['删除信息', 'url', 'Common.list_delete'];				
		var a2 = ['打印任务单', 'url', 'Common.dialog_txt', '650x400'];
		var a9 = ['导出Excel', 'url', 'jswin', '1024x768'];

		var s = c1 + ", " + c2 + ", " + c9;

		return popup(e, [a1, a2, a9], s);
	}
};
